package com.rd.student.jike.class2.lihongming.Solutions5;

import javax.swing.text.html.HTMLDocument;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @Auther:Heinrich
 * @Date:2023/7/31-07-31-16:13
 * @Description:com.rd.student.jike.class2.lihongming.Solutions5
 * @version:1.0
 */
public class Question6_1 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要随机的人数");
        int order  =   sc.nextInt();
        System.out.println(randomName(order));

    }
    public static HashSet<String> randomName(int nameNum) throws IOException{
        HashSet<String> result = new HashSet<>(nameNum);
        HashSet<String> names = new HashSet<>(Arrays.asList(reader("all.txt").split("、")));
        HashSet<String> random = new HashSet<>(Arrays.asList(reader("random.txt").split("、")));
        names.removeAll(random);
        HashSet<String> leave = new HashSet<>(Arrays.asList(reader("leave.txt").split("、")));
        names.removeAll(leave);
        String[] names2 = names.toArray(new String[0]);
        while (result.size() < nameNum) {
            int index = (int) (Math.random() * names.size());
            result.add(names2[index]);
        }
        writer("random.txt", result, true);
        return result;
    }
    public static String reader(String path) throws IOException {
        File fileRandom =  new File(path);
        FileInputStream isRandom = new FileInputStream(fileRandom);
        byte[]  pipe = new byte[(int) fileRandom.length()];
        isRandom.read(pipe);
        isRandom.close();
        return new String(pipe);
    }
    public static void writer(String path, HashSet<String> names, boolean concat) throws IOException {
        File file = new File(path);
        FileOutputStream os = new FileOutputStream(file,concat);
        Iterator<String> it = names.iterator();
        while (it.hasNext()){
            String name = it.next() +  "、";
            byte[] pipe  = name.getBytes();
            os.write(pipe);
        }
        os.close();
    }
}
